<template>
  <div class="pv">
    <el-row>
      <el-col :span="24">
        <div>
          <el-card
            class="pv-head-card"
            shadow="never"
            :body-style="{ padding: '10px' }"
          >
            <el-row>
              <el-col :span="2">
                <div>
                  <el-button
                    disabled
                    style="border-radius: 2px"
                    icon="Edit"
                    type="primary"
                    >创建</el-button
                  >
                </div>
              </el-col>
              <el-col :span="6">
                <div>
                  <el-input
                    v-model="searchQuery.name"
                    class="pv-head-search"
                    clearable
                    placeholder="请输入"
                  />
                  <el-button
                    style="border-radius: 2px"
                    icon="Search"
                    type="primary"
                    plain
                    @click="getPVs()"
                    >搜索</el-button
                  >
                </div>
              </el-col>
              <el-col :span="2" :offset="14">
                <div>
                  <el-button
                    style="border-radius: 2px"
                    icon="Refresh"
                    plain
                    @click="getPVs()"
                    >刷新</el-button
                  >
                </div>
              </el-col>
            </el-row>
          </el-card>
        </div>
      </el-col>
      <el-col :span="24">
        <div>
          <el-card
            class="pv-body-card"
            shadow="never"
            :body-style="{ padding: '5px' }"
          >
            <el-table
              v-loading="loading"
              style="width: 100%; font-size: 12px; margin-bottom: 10px"
              :data="pvList"
            >
              <el-table-column width="20" />
              <el-table-column align="left" label="PV名">
                <template v-slot="scope">
                  <a class="pv-body-pvname">{{ scope.row.metadata.name }}</a>
                </template>
              </el-table-column>
              <el-table-column align="center" label="状态">
                <template v-slot="scope">
                  <span
                    :class="[
                      scope.row.status.phase === 'Bound'
                        ? 'success-status'
                        : 'error-status'
                    ]"
                    >{{ scope.row.status.phase }}</span
                  >
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="spec.accessModes[0]"
                label="访问模式"
              />
              <el-table-column
                align="center"
                prop="spec.capacity.storage"
                label="容量"
              />
              <el-table-column
                align="center"
                prop="spec.claimRef.name"
                label="PVC"
              />
              <el-table-column align="center" min-width="100" label="创建时间">
                <template v-slot="scope">
                  <el-tag type="info"
                    >{{ timeTrans(scope.row.metadata.creationTimestamp) }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column align="center" label="操作" min-width="120">
                <template v-slot="scope">
                  <el-button
                    size="small"
                    style="border-radius: 2px"
                    icon="Edit"
                    type="primary"
                    plain
                    @click="getPVDetails(scope)"
                    >YAML</el-button
                  >
                  <el-button
                    size="small"
                    style="border-radius: 2px"
                    icon="Delete"
                    type="danger"
                    @click="handleConfirm(scope, '删除', deletePV)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              class="pv-body-pagination"
              background
              :current-page="pager.currentPage.value"
              :page-sizes="pager.limitList.value"
              :page-size="pager.limit.value"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pager.total.value"
            />
          </el-card>
        </div>
      </el-col>
    </el-row>
    <el-dialog v-model="yamlDialog" title="YAML信息" width="45%" top="5%">
      <codemirror
        :value="srcYamlContent"
        border
        :options="cmOptions"
        height="500"
        style="font-size: 14px"
        @change="onChange"
      />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="yamlDialog = false">取 消</el-button>
          <el-button disabled type="primary" @click="updatePV()">
            更 新
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import common from "../common/Config";
import provider from "./pvProvider";
import nsProvider from "../namespace/nsProvider";

import {
  ellipsis,
  timeTrans,
  handleConfirm
} from "@/views/domain/kubeHud/utils";
import { ref } from "vue";
import { ElMessageBox } from "element-plus";
import { load } from "@amap/amap-jsapi-loader";

const { cmOptions } = common;

const { nsList } = nsProvider;

const {
  pvList,
  getPVs,
  getPVDetails,
  updatePV,
  deletePV,
  yamlDialog,
  srcYamlContent,
  loading,
  searchQuery,
  pager
} = provider;

// 新建deploy内容
const newPV = ref<Record<string, any>>({});

const onChange = val => {
  srcYamlContent.value = val;
};

const createPVDrawer = ref(false);
const fullScreenLoading = ref(false);

const onDrawEditCancel = (done: any) => {
  ElMessageBox.confirm("确定要关闭吗？").then(() => {
    done && done();
  });
};

const onApplyCreateDeploy = (info: any) => {};

/*
import common from "../common/Config";
import yaml2obj from "js-yaml";
import json2yaml from "json2yaml";
import {
  DeletePersistentVolume,
  GetPersistentVolume,
  GetPersistentVolumeDetail,
  UpdatePersistentVolume
} from "@/views/domain/kubeHud/api/k8s/pv";
export default {
  data() {
    return {
      //编辑器配置
      cmOptions: common.cmOptions,
      contentYaml: "",
      //分页
      currentPage: 1,
      pagesize: 10,
      pagesizeList: [10, 20, 30],
      //
      searchInput: "",
      namespaceValue: "default",
      namespaceList: [],
      namespaceListUrl: common.k8sNamespaceList,
      appLoading: false,
      pvList: [],
      pvTotal: 0,
      getPvsData: {
        url: common.k8sPvList,
        params: {
          filter_name: "",
          namespace: "",
          page: "",
          limit: ""
        }
      },
      //详情
      pvDetail: {},
      getPvDetailData: {
        url: common.k8sPvDetail,
        params: {
          name: "",
          namespace: ""
        }
      },
      //yaml更新
      yamlDialog: false,
      updatePvData: {
        url: common.k8sPvUpdate,
        params: {
          namespace: "",
          content: ""
        }
      },
      //删除
      delPvData: {
        url: common.k8spvDel,
        params: {
          name: "",
          namespace: ""
        }
      }
    };
  },
  beforeMount() {
    this.getPvs();
  },
  methods: {
    transYaml(content) {
      return json2yaml.stringify(content);
    },
    transObj(content) {
      return yaml2obj.load(content);
    },
    onChange(val) {
      this.contentYaml = val;
    },
    handleSizeChange(size) {
      this.pagesize = size;
      this.getPvs();
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.getPvs();
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
    ellipsis(value) {
      return value.length > 15 ? value.substring(0, 15) + "..." : value;
    },
    timeTrans(timestamp) {
      let date = new Date(new Date(timestamp).getTime() + 8 * 3600 * 1000);
      date = date.toJSON();
      date = date.substring(0, 19).replace("T", " ");
      return date;
    },
    specTrans(str) {
      if (str.indexOf("Ki") == -1) {
        return str;
      }
      let num = str.slice(0, -2) / 1024 / 1024;
      return num.toFixed(0);
    },
    getNamespaces() {
      getNamespecelist()
        .then(res => {
          this.namespaceList = res.data.items;
        })
        .catch(res => {
          this.$message.error({
            message: res.msg
          });
        });
    },
    getPvs() {
      this.appLoading = true;
      this.getPvsData.params.filter_name = this.searchInput;
      this.getPvsData.params.namespace = this.namespaceValue;
      this.getPvsData.params.page = this.currentPage;
      this.getPvsData.params.limit = this.pagesize;
      GetPersistentVolume(this.getPvsData.params)
        .then(res => {
          this.pvList = res.data.items;
          this.pvTotal = res.data.total;
        })
        .catch(res => {
          this.$message.error({
            message: res.msg
          });
        });
      this.appLoading = false;
    },
    getPvDetail(e) {
      this.getPvDetailData.params.name = e.row.metadata.name;
      this.getPvDetailData.params.namespace = this.namespaceValue;
      GetPersistentVolumeDetail(this.getPvDetailData.params)
        .then(res => {
          this.pvDetail = res.data;
          this.contentYaml = this.transYaml(this.pvDetail);
          this.yamlDialog = true;
        })
        .catch(res => {
          this.$message.error({
            message: res.msg
          });
        });
    },
    updatePv() {
      let content = JSON.stringify(this.transObj(this.contentYaml));
      this.updatePvData.params.namespace = this.namespaceValue;
      this.updatePvData.params.content = content;
      UpdatePersistentVolume(this.updatePvData.params)
        .then(res => {
          this.$message.success({
            message: res.msg
          });
        })
        .catch(res => {
          this.$message.error({
            message: res.msg
          });
        });
      this.yamlDialog = false;
    },
    delPv(e) {
      this.delPvData.params.name = e.row.metadata.name;
      this.delPvData.params.namespace = this.namespaceValue;
      DeletePersistentVolume(this.delPvData.params)
        .then(res => {
          this.getPvs();
          this.$message.success({
            message: res.msg
          });
        })
        .catch(res => {
          this.$message.error({
            message: res.msg
          });
        });
    },
    handleConfirm(obj, operateName, fn) {
      this.confirmContent = "确认继续 " + operateName + " 操作吗？";
      this.$confirm(this.confirmContent, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(() => {
          fn(obj);
        })
        .catch(() => {
          this.$message.info({
            message: "已取消操作"
          });
        });
    }
  }
};
*/
</script>

<style scoped>
.pv-head-card,
.pv-body-card {
  border-radius: 1px;
  margin-bottom: 5px;
}
.pv-head-search {
  width: 160px;
  margin-right: 10px;
}
.pv-body-pvname {
  color: #4795ee;
}
.pv-body-pvname:hover {
  color: rgb(84, 138, 238);
  cursor: pointer;
  font-weight: bold;
}
.success-status {
  color: rgb(27, 202, 21);
}
.warning-status {
  color: rgb(233, 200, 16);
}
.error-status {
  color: rgb(226, 23, 23);
}
</style>
